#!/usr/bin/env bash

source ./init.sh

prepare() {
	apt-get install -y ${TENGINE_REQUIRE_PKGS}
	download_uncompress ${TENGINE_TEMP_DIR} ${PCRE_NAME} ${PCRE_SOURCE}
	download_uncompress ${TENGINE_TEMP_DIR} ${ZLIB_NAME} ${ZLIB_SOURCE}
	download_uncompress ${TENGINE_TEMP_DIR} ${OPENSSL_NAME} ${OPENSSL_SOURCE}
}

setup() {
	download_uncompress ${TENGINE_TEMP_DIR} ${TENGINE_NAME} ${TENGINE_SOURCE}
	cd "${TENGINE_TEMP_DIR}/${TENGINE_NAME}"
	${TENGINE_CONF_CMD}
	make && make install
	mk_config
	# try to add site default
	add_site "site_php" default
}

mk_config() {

	if [ ! -d "${TENGINE_HOME}" ]; then
		echo "Tengine home ${TENGINE_HOME} does not exist!"
		return 1;
	fi

	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi

	tengine_config_basic
	mk_tengine_script
	tengine_test
}

tengine_config_basic() {
	mk_tengine_conf
	mk_tengine_deny_settings
	cp_tengine_common
	mk_tengine_require_dir
}

mk_tengine_require_dir() {
	###########################################################
	# make sure the other dirs are existing
	###########################################################
	for dir in ${TENGINE_REQUIRE_DIRS[@]}; do
		if [ ! -d "${dir}" ]; then
			echo "Creating dir ${dir}"
			mkdir "${dir}"
		fi
	done
}

mk_tengine_conf() {
	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi
	local name=tengine.conf
	local savefile=${TENGINE_CONF_DIR}/${name}
	if [ -f ${savefile} ]; then
		echo "Delete ${savefile}"
		rm ${savefile} -f
	fi
	local tengine_conf_tpl=${CUR_DIR}/tengine/tengine.conf.tpl
	IFS=""
	local tengine_conf=$(read_file ${tengine_conf_tpl})
	tengine_conf=${tengine_conf//\#WWW_USER\#/${WWW_USER}}
	tengine_conf=${tengine_conf//\#WWW_GROUP\#/${WWW_GROUP}}
	tengine_conf=${tengine_conf//\#WORK_PROS\#/${TENGINE_WORK_PROS}}
	tengine_conf=${tengine_conf//\#PID_FILE\#/${TENGINE_PID_FILE}}
	tengine_conf=${tengine_conf//\#RLIMIT\#/${TENGINE_RLIMIT}}
	tengine_conf=${tengine_conf//\#WORKER_CONNS\#/${TENGINE_WORKER_CONNS}}
	tengine_conf=${tengine_conf//\#CONF_DIR\#/${TENGINE_CONF_DIR}}
	tengine_conf=${tengine_conf//\#DEFAULT_MIME_TYPE\#/${TENGINE_DEFAULT_MIME_TYPE}}
	tengine_conf=${tengine_conf//\#DEFAULT_CHARSET\#/${TENGINE_DEFAULT_CHARSET}}
	echo ${tengine_conf} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mk_tengine_script() {
	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi
	local savefile=/etc/init.d/tengine
	if [ -f ${savefile} ]; then
		echo "Delete ${savefile}"
		rm ${savefile} -f
	fi
	local tengine_sh_tpl=${CUR_DIR}/tengine/tengine.sh.tpl
	IFS=""
	local tengine_sh=$(read_file ${tengine_sh_tpl})
	tengine_sh=${tengine_sh//\#PID_FILE\#/${TENGINE_PID_FILE}}
	## dump file
	echo ${tengine_sh} > ${savefile}
	# make it executable
	chmod +x ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mk_php_settings() {
	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi
	local fcgipass=${1}
	local savename=php_settings

	if [ ! -n "${fcgipass}" ]; then
		fcgipass=${PHP_FPM_LISTEN}
	fi

	if [ -n "${2}" ]; then
		savename=${2}
	fi

	local savefile=${TENGINE_CONF_DIR}/${savename}.conf
	if [ -f ${savefile} ]; then
		echo "Delete ${savefile}"
		rm ${savefile} -f
	fi
	local tpl=${CUR_DIR}/tengine/php_settings.conf.tpl
	IFS=""
	local data=$(read_file ${tpl})
	data=${data//\#FASTCGI_PASS\#/${fcgipass}}
	data=${data//\#CONF_DIR\#/${TENGINE_CONF_DIR}}
	echo ${data} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mk_php_common_rewrite() {
	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi
	local name=php_common_rewrite.conf
	local savefile=${TENGINE_CONF_DIR}/${name}
	if [ -f ${savefile} ]; then
		echo "Delete ${savefile}"
		rm ${savefile} -f
	fi
	local tpl=${CUR_DIR}/tengine/php_common_rewrite.conf.tpl
	IFS=""
	local data=$(read_file ${tpl})
	echo ${data} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mk_tengine_deny_settings() {
	if [ ! -d "${TENGINE_CONF_DIR}" ]; then
		echo "Tengine conf dir ${TENGINE_CONF_DIR} does not exist!"
		return 1;
	fi
	local name=deny_settings.conf
	local savefile=${TENGINE_CONF_DIR}/deny_settings.conf
	if [ -f ${savefile} ]; then
		echo "Delete ${savefile}"
		rm ${savefile} -f
	fi
	local tpl=${CUR_DIR}/tengine/deny_settings.conf.tpl
	IFS=""
	local data=$(read_file ${tpl})
	## dump file
	echo ${data} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

cp_tengine_common() {
	echo "Coping common config files..."
	if [ ! -d "${TENGINE_CONF_DIR}/common" ]; then
		mkdir "${TENGINE_CONF_DIR}/common"
	fi

	cp ${CUR_DIR}/tengine/common/*.conf ${TENGINE_CONF_DIR}/common
}

tengine_test() {
	local CMD=/etc/init.d/tengine
	${CMD} test
}

mk_www_home() {
	if [ ! -d "${TENGINE_WWW_HOME}" ]; then
		echo "Creating ${TENGINE_WWW_HOME} ..."
		mkdir "${TENGINE_WWW_HOME}"
	fi

	chown ${WWW_USER}:${WWW_GROUP} "${TENGINE_WWW_HOME}" -Rf
}

add_site() {
	mk_www_home
	local tplname=$1
	local sitename=$2
	local port=$3
	local sitedir=$4
	local conf=${TENGINE_VHOST_CONF_DIR}/${sitename}.conf

	if [ ! -n "${sitename}" ]; then
		echo "The sitename can't be empty!!"
		return 1
	fi

	if [ ! -n "${sitedir}" ]; then
		sitedir=${TENGINE_WWW_HOME}/${sitename}
	fi

	if [ ! -n "${port}" ]; then
		port=80
	fi

	# if config file is exising, we will exit
	if [ -f "${conf}" ]; then
		echo "The site ${sitename}:${conf} is existing! Please mv or rm ${conf}!"
		return 1
	fi

	case "${tplname}" in
		site_php)
			mk_php_settings
			mk_php_common_rewrite
			;;
	esac

	# make sure the dir is existing
	if [ ! -d "${sitedir}" ]; then
		mkdir "${sitedir}"
	fi
	# change it belong to wwwuser and wwwgroup
	chown ${WWW_USER}:${WWW_GROUP} "${sitedir}" -Rf

	local tpl=${CUR_DIR}/tengine/${tplname}.conf.tpl
	IFS=""
	local data=$(read_file ${tpl})
	data=${data//\#VHOST_LOG_DIR\#/${TENGINE_VHOST_LOG_DIR}}
	data=${data//\#SITE_NAME\#/${sitename}}
	data=${data//\#SITE_DIR\#/${sitedir}}
	data=${data//\#CONF_DIR\#/${TENGINE_CONF_DIR}}
	data=${data//\#PORT\#/${port}}
	echo ${data} > ${conf}

	if [ -f ${conf} ]; then
		echo "Create ${conf} success!"
		tengine_test
		return 0
	else
		echo "Create ${conf} lost!"
		return 1
	fi
}

case "$1" in
	install)
		if [ -d "${TENGINE_HOME}" ]; then
			echo "Tengine has been setuped!! Please try rebuild!!"
		else
			prepare
			setup
		fi
		;;
	rebuild)
		prepare
		setup
		;;
	conf|conf-all)
		mk_config
		;;
	conf-main)
		mk_tengine_conf
		mk_tengine_require_dir
		tengine_test
		;;
	conf-deny)
		mk_tengine_deny_settings
		tengine_test
		;;
	conf-common)
		cp_tengine_common
		tengine_test
		;;
	conf-php)
		mk_php_settings $2 $3
		mk_php_common_rewrite
		;;
	script|sh)
		mk_tengine_script
		tengine_test
		;;
	test)
		tengine_test
		;;
	ensite)
		add_site "site" $2 $3 $4
		;;
	enphpsite)
		add_site "site_php" $2 $3 $4
		;;
#	php-config)
#		fcgipass=$2
#		;;
#	upstream)
#		domain=$1
#		servers=$2
#		;;
	*)
		echo "Usage: ./tengine.sh {install|rebuild|conf|conf-all|conf-main|conf-deny|conf-common|conf-php|script|sh|test|ensite|enphpsite}"
		;;
esac

on_tail